﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using sauyou.Utils;

namespace sauyou.Admin
{
    public partial class Article_Add : ManagePage
    {
        /// <summary>
        /// 编号
        /// </summary>
        private string mDataId
        {
            get
            {
                object obj = (object)ViewState["mdataId"];
                if (obj != null)
                    return (string)obj;
                else
                    return "";
            }
            set { ViewState["mdataId"] = value; }
        }

        public sauyou.DAL.Article bll = new sauyou.DAL.Article();
        public sauyou.Model.Article model = new sauyou.Model.Article();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                mDataId = Request["dataId"];
                //绑定类别
                ChannelTreeBind(0, "请选择所属类别...", (int)Channel.Article, this.ddlClassId);
                if (!string.IsNullOrEmpty(mDataId))
                {
                    chkLoginLevel("editArticle");
                    ShowInfo(Convert.ToInt32(mDataId));
                }
                else
                {
                    chkLoginLevel("addArticle");                    
                    if (!string.IsNullOrEmpty(Request.Params["classId"]))
                    {
                        ddlClassId.SelectedValue = Request.Params["classId"].Trim();
                    }
                }

            }
        }

        //赋值操作
        private void ShowInfo(int _id)
        {
            sauyou.DAL.Article bll = new sauyou.DAL.Article();
            sauyou.Model.Article model = bll.GetModel(_id);

            txtTitle.Text = model.Title;
            txtAuthor.Text = model.Author;
            txtForm.Text = model.Form;
            txtKeyword.Text = model.Keyword;
            txtZhaiyao.Text = model.Zhaiyao;
            ddlClassId.SelectedValue = model.ClassId.ToString();
            txtImgUrl.Text = model.ImgUrl;
            txtDaodu.Text = model.Daodu;
            txtContent.Value = model.Content;
            txtClick.Text = model.Click.ToString();
            if (model.IsMsg == 1)
            {
                cblItem.Items[0].Selected = true;
            }
            if (model.IsTop == 1)
            {
                cblItem.Items[1].Selected = true;
            }
            if (model.IsRed == 1)
            {
                cblItem.Items[2].Selected = true;
            }
            if (model.IsHot == 1)
            {
                cblItem.Items[3].Selected = true;
            }
            if (model.IsSlide == 1)
            {
                cblItem.Items[4].Selected = true;
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            model.Title = txtTitle.Text.Trim();
            model.Author = txtAuthor.Text.Trim();
            model.Form = txtForm.Text.Trim();
            //自动提取关健字
            if (txtKeyword.Text.Trim() != string.Empty)
            {
                model.Keyword = txtKeyword.Text.Trim();
            }
            else
            {
                model.Keyword = txtTitle.Text.Trim();
            }
            //自动提取摘要
            if (txtZhaiyao.Text.Trim() != string.Empty)
            {
                model.Zhaiyao = sauyou.Utils.Utils.DropHTML(txtZhaiyao.Text, 250);
            }
            else
            {
                model.Zhaiyao = sauyou.Utils.Utils.DropHTML(txtContent.Value, 250);
            }
            //自动提取导读
            if (txtDaodu.Text.Trim() != string.Empty)
            {
                model.Daodu = sauyou.Utils.Utils.DropHTML(txtDaodu.Text, 250);
            }
            else
            {
                model.Daodu = sauyou.Utils.Utils.DropHTML(txtContent.Value, 250);
            }
            model.ClassId = int.Parse(ddlClassId.SelectedValue);
            model.ImgUrl = txtImgUrl.Text.Trim();
            model.Content = txtContent.Value;
            model.Click = int.Parse(txtClick.Text.Trim());

            model.IsMsg = 0;
            model.IsTop = 0;
            model.IsRed = 0;
            model.IsHot = 0;
            model.IsSlide = 0;
            if (cblItem.Items[0].Selected == true)
            {
                model.IsMsg = 1;
            }
            if (cblItem.Items[1].Selected == true)
            {
                model.IsTop = 1;
            }
            if (cblItem.Items[2].Selected == true)
            {
                model.IsRed = 1;
            }
            if (cblItem.Items[3].Selected == true)
            {
                model.IsHot = 1;
            }
            if (cblItem.Items[4].Selected == true)
            {
                model.IsSlide = 1;
            }
            if (!string.IsNullOrEmpty(mDataId))
            {
                model.Id = Convert.ToInt32(mDataId);
                bll.Update(model);
                //保存日志
                SaveLogs("[资讯模块]编辑文章：" + model.Title);
                JscriptPrint("文章编辑成功啦！", "Article_List.aspx", "Success");
            }
            else
            {
                bll.Add(model);
                //保存日志
                SaveLogs("[资讯模块]添加文章：" + model.Title);
                JscriptPrint("文章发布成功啦！", "Article_Add.aspx?classId=" + ddlClassId.SelectedValue, "Success");
            }

        }
    }
}
